//package com.yenlien.traceyun.plant.service;
//
//import java.util.ArrayList;
//import java.util.List;
//import java.util.UUID;
//
//import javax.persistence.criteria.CriteriaBuilder;
//import javax.persistence.criteria.CriteriaQuery;
//import javax.persistence.criteria.Path;
//import javax.persistence.criteria.Predicate;
//import javax.persistence.criteria.Root;
//
//import com.yenlien.traceyun.business.service.rest.usercenter.CompanyRestService;
//import com.yenlien.traceyun.plant.dao.*;
//import com.yenlien.traceyun.plant.entity.*;
//import com.yenlien.traceyun.webase.exception.BusinessException;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.domain.Page;
//import org.springframework.data.domain.PageRequest;
//import org.springframework.data.domain.Pageable;
//import org.springframework.data.domain.Sort;
//import org.springframework.data.jpa.domain.Specification;
//import org.springframework.jdbc.core.JdbcTemplate;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import com.alibaba.fastjson.JSONObject;
//import com.yenlien.traceyun.common.utils.Tools;
//
//@Service
//public class SubpackageService {
//	@Autowired
//	private SubpackageDao subpackageDao;
//	@Autowired
//	private ProcessDetailDao processDetailDao;
//	@Autowired
//	private StorageManageDao storageManageDao;
//	@Autowired
//	private StorageManageDetailedDao storageManageDetailedDao;
//	@Autowired
//	private BoxDao boxDao;
//	@Autowired
//	private CompanyRestService companyRestService;
//	@Autowired
//	private PrintMsgDao printMsgDao;
//	@Autowired
//	private JdbcTemplate jdbc;
//
//	public Page<Subpackage> findByList(final Subpackage subpackage) {
//		Pageable pageable = PageRequest.of(subpackage.getPage(), subpackage.getPageSize(), Sort.Direction.DESC, ("createtime"));
//        return subpackageDao.findAll(new Specification<Subpackage>() {
//            private static final long serialVersionUID = -1396496630802017658L;
//            @Override
//            public Predicate toPredicate(Root<Subpackage> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
//                Path<String> source = root.get("source");
//                Path<String> productName = root.get("productName");
//                //构造查询条件
//                List<Predicate> list = new ArrayList<Predicate>();
//                if (!StringUtils.isEmpty(subpackage.getSource())) {
//					list.add(cb.equal(source, subpackage.getSource()));
//				}
//                Predicate[] predicateA = new Predicate[list.size()];
//                Predicate predicateAnd = cb.and(list.toArray(predicateA));
//
//                if (StringUtils.isNotEmpty(subpackage.getProductName())) {
//                    List<Predicate> list2 = new ArrayList<>();
//                    list2.add(cb.like(productName, "%" + subpackage.getProductName() + "%"));
//                    Predicate[] predicateO = new Predicate[list2.size()];
//                    Predicate predicateOr = cb.or(list2.toArray(predicateO));
//                    return query.where(predicateAnd, predicateOr).getRestriction();
//                } else {
//                    return predicateAnd;
//                }
//            }
//        }, pageable);
//	}
//
//	@Transactional
//	public void save(Subpackage subpackage) {
//
//		String source = subpackage.getSource();
//		if (("生产分包").equals(source)) {
//			ProcessDetail processDetail = processDetailDao.findById(subpackage.getRelevanceId());
//			processDetail.setSurplusNumber(Tools.subtr(processDetail.getSurplusNumber(), subpackage.getWeight()));
//			processDetailDao.save(processDetail);
//
//			subpackage.setProductName(processDetail.getProductName());
//			subpackage.setBatch(processDetail.getBatch());
//			subpackage.setGrade(processDetail.getGrade());
//		} else if (("采购分包").equals(source)) {
//			StorageManage storageManage = storageManageDao.findById(subpackage.getRelevanceId());
//			String weight = subpackage.getWeight();
//			String bag = Tools.division(weight, storageManage.getSpecs());
//
//			storageManage.setSubtotal(Tools.subtr(storageManage.getSubtotal(), weight));
//			storageManage.setSurplus(Tools.subtr(storageManage.getSurplus(), bag));
//			storageManageDao.save(storageManage);
//
//			StorageManageDetailed storageManageDetailed = new StorageManageDetailed();
//			storageManageDetailed.setStorageManageId(storageManage.getId());
//			storageManageDetailed.setType("分包");
//			storageManageDetailed.setSpecs(storageManage.getSpecs());
//			storageManageDetailed.setNumber(bag);
//			storageManageDetailedDao.save(storageManageDetailed);
//
//			subpackage.setProductName(storageManage.getProductName());
//			subpackage.setBatch(storageManage.getStorageBatch());
//			subpackage.setGrade(storageManage.getSupplierName());
//		}
//		if (subpackage.getIsOk() == 1) {
//			subpackage.setTotalNumber((int) Math.ceil(subpackage.getSubNumber() * 1.05));
//		} else {
//			subpackage.setTotalNumber(subpackage.getSubNumber());
//		}
//		subpackageDao.save(subpackage);
//
////		List<Box> boxList = new ArrayList<>();
////		for (int i = 0; i < subpackage.getTotalNumber(); i++) {
////			Box box = new Box();
////			box.setPlatform("生产");
////			box.setProductName(subpackage.getProductName());
////			box.setSubpackageId(subpackage.getId());
////			box.setSourceCode(Tools.random(20));
////			boxList.add(box);
////		}
////		boxDao.saveAll(boxList);
//	}
//
//	public List<Subpackage> findByRelevanceId(String relevanceId) {
//		return subpackageDao.findByRelevanceId(relevanceId);
//	}
//
//	@Transactional
//	public String print(JSONObject params) {
//		Subpackage subpackage = subpackageDao.findById(params.getString("subpackageId"));
//		if (subpackage == null) {
//			throw new BusinessException("A0001", "数据有误");
//		}
//		if (subpackage.getSubNumber() < params.getInteger("num")) {
//			throw new BusinessException("A0001", "剩余打码量不足");
//		}
//		subpackage.setPrintNumber(subpackage.getPrintNumber() + params.getIntValue("num"));
//		subpackage.setSubNumber(subpackage.getSubNumber() - params.getInteger("num"));
//		subpackageDao.save(subpackage);
//
//		PrintMsg printMsg = new PrintMsg();
//		printMsg.setSubpackageId(params.getString("subpackageId"));
//		printMsg.setContent(params.getString("content"));
//		printMsg.setType(params.getString("type"));
//		JSONObject json = new JSONObject();
//		printMsgDao.save(printMsg);
//		return printMsg.getId();
//	}
//
//	public JSONObject findPrintMsg(String subpackageId) {
//		Subpackage subpackage = subpackageDao.findById(subpackageId);
//		JSONObject json = new JSONObject();
//		json.put("productName", subpackage.getProductName());
//		json.put("batch", subpackage.getBatch());
//		json.put("spec", subpackage.getSpec());
//		json.put("specUnits", subpackage.getSpecUnits());
//
//		JSONObject obj = (JSONObject) companyRestService.findById(subpackage.getCompanyId());
//		json.put("company", obj.get("name"));
//
//		return json;
//	}
//
//	//在线打印
//	public JSONObject onLinePrint(String printMsgId, String subpackageId, int num, String companyId) {
//		JSONObject json = new JSONObject();
//		PrintMsg printMsg = printMsgDao.findById(printMsgId);
//		List<Box> boxList = new ArrayList<>();
//		List<String> list = new ArrayList<>();
//		//=================新增码表数据开始=================
//		String sql = "insert into t_plant_box (id,last_step,source_code,subpackage_id,print_msg_id,company_id)" +
//				" values (?,?,?,?,?,?)";
//		List<Object[]> batchArgs = new ArrayList<Object[]>();
//		for (int i = 0; i < num; i++) {
//			String code = Tools.random(20);
//			list.add(code);
//			batchArgs.add(new Object[]{UUID.randomUUID().toString().replaceAll("-",""),printMsg.getLastStep(),code,
//					printMsg.getSubpackageId(),printMsgId,companyId});
//		}
//		jdbc.batchUpdate(sql, batchArgs);
//		//=================新增码表数据结束=================
//		json.put("code","200");
//		json.put("printMsg",printMsg);
//		json.put("boxList",list);
//		return json;
//	}
//
//	@Transactional
//	public List<String> getCode(JSONObject params) {
//		Subpackage subpackage = subpackageDao.findById(params.getString("subpackageId"));
//		if (subpackage == null) {
//			throw new BusinessException("A0001", "数据有误");
//		}
//		if (subpackage.getSubNumber() < params.getInteger("num")) {
//			throw new BusinessException("A0001", "剩余打码量不足");
//		}
//		subpackage.setPrintNumber(subpackage.getPrintNumber() + params.getIntValue("num"));
//		subpackage.setSubNumber(subpackage.getSubNumber() - params.getInteger("num"));
//		subpackageDao.save(subpackage);
//
//		PrintMsg printMsg = new PrintMsg();
//		printMsg.setSubpackageId(params.getString("subpackageId"));
//		printMsg.setContent(params.getString("content"));
//		printMsg.setType(params.getString("type"));
//		printMsgDao.save(printMsg);
//
//		List<Box> boxList = new ArrayList<>();
//		List<String> list = new ArrayList<>();
//		//=================新增码表数据开始=================
//		String sql = "insert into t_plant_box (id,last_step,source_code,subpackage_id,print_msg_id,company_id)" +
//				" values (?,?,?,?,?,?)";
//		List<Object[]> batchArgs = new ArrayList<Object[]>();
//		for (int i = 0; i < params.getInteger("num"); i++) {
//			//生成离线码
//			String code = Tools.random(20);
//			list.add(code);
//			batchArgs.add(new Object[]{UUID.randomUUID().toString().replaceAll("-",""),printMsg.getLastStep(),code,
//					printMsg.getSubpackageId(),printMsg.getId(),params.getString("companyId")});
//		}
//		jdbc.batchUpdate(sql, batchArgs);
//		//=================新增码表数据结束=================
//		return list;
//	}
//}
